package com.fzebu.fastboot.util;

import com.weaver.general.GCONSTUClient;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: Jack
 * @create: 2019/12/9 22:05
 * @version: 1.0.0
 **/
public class PropUtil {

    private static final String ROOT_PATH = GCONSTUClient.getPropertyPath();

    private static final Map<String, Properties> PROP_MAP = new ConcurrentHashMap<>();

    public static Properties load(String filename) {
        if (PROP_MAP.containsKey(filename)) {
            return PROP_MAP.get(filename);
        }
        String filepath = ROOT_PATH + filename +".properties";
        File file = new File(filepath);
        if (!file.exists()) {
            return null;
        }
        Properties prop = new Properties();
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            prop.load(new InputStreamReader(bis, StandardCharsets.UTF_8));
            PROP_MAP.put(filename, prop);
            return prop;
        } catch (IOException e) {
            return null;
        } finally {
            prop.clone();
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}